package com.zhongc.advance.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.zhongc.advance.dao.PassWordDao;
import com.zhongc.advance.dao.UserDao;
import com.zhongc.advance.dao.UserloginLogDao;
import com.zhongc.advance.info.User;
import com.zhongc.advance.info.UserloginLog;
import com.zhongc.advance.order.LoginOrder;
import com.zhongc.advance.service.AdvanceUserService;

@Repository
public class AdvanceUserServiceImpl implements AdvanceUserService {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private UserDao userDao;

	@Autowired
	private PassWordDao passWordDao;

	@Autowired
	private UserloginLogDao loginLog;

	@Override
	@Transactional
	public int register(User user) {
		logger.info("注册会员入参  User ->{}",user);
		int i =userDao.register(user);
		int n = passWordDao.addPassWord(user.getUserId(), user.getPassWord());
		if (i>0 && n>0) {
			return 1;
		}
		return 0;

	}
	@Override
	public User login(LoginOrder order) {
		logger.info("会员登陆 入参  LoginOrder ->{}",order);
		User us = userDao.login(order.getUserName(), order.getPassWord());
		if (us!=null) {
			UserloginLog log = new UserloginLog();
			log.setUserId(us.getUserId());
			log.setLoginFrom(order.getLoginFrom());
			log.setLoginTime(order.getLoginTime());
			log.setLoginIp(order.getIp());
			loginLog.addLoginLog(log);
		}
		return us;
	}
	@Override
	public int queryUserByUserId(String userId) {
		return 0;
	}

	@Override
	public List<User> queryUser(Map<String, Object>... maps) {
		return null;
	}

	@Override
	public User queryUserByUsername(String userName) {
		return userDao.queryUserByUsername(userName);
	}

	@Override
	public int updateUser(User user) {
		return 0;
	}

	@Override
	public int updatePassword(String userId, String passWord) {
		return 0;
	}

	public int addPassWord(String userId,String passWord){
		return passWordDao.addPassWord(userId, passWord);
	}


}
